﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public class Spawner:Singleton<Spawner>
{
    public static List<Vector3> mapRoads = null;
    static GameObject mapPre ;
    static Dictionary<Vector3,GameObject> map_boxs = new Dictionary<Vector3, GameObject>();
    static Dictionary<Vector3, Tower> towers = new Dictionary<Vector3, Tower>();
    public static List<GameObject> monsters = new List<GameObject>();
    public static List<GameObject> bullets = new List<GameObject>();
    //private void Start()
    //{
    //    mapRoads = MapManage.LoadPath("1");
    //    mapPre = Resources.Load<GameObject>("Model/map_box");
    //}
    private void Update()
    {
    }
    #region 绘制地图和清除地图
    //绘制地图
    public static void MapDraw()
    {
        Vector3 pos = mapRoads[0];
        SpawnMapTile(pos);
        for (int i=1;i<mapRoads.Count;i++)
        {
            while(pos.x<mapRoads[i].x)
            {
                pos.x++;
                SpawnMapTile(pos);
            }
            while (pos.x > mapRoads[i].x)
            {
                pos.x--;
                SpawnMapTile(pos);
            }
            while (pos.z < mapRoads[i].z)
            {
                pos.z++;
                SpawnMapTile(pos);
            }
            while (pos.z > mapRoads[i].z)
            {
                pos.z--;
                SpawnMapTile(pos);
            }
        }
    }
    #endregion
    //板砖初始化
    public static void  SpawnMapTile(Vector3 pos)
    {
        GameObject box = Pools.Instance.Spawn("map_box");
        box.transform.localPosition = pos;
        box.transform.localScale = Vector3.one;
        box.transform.localRotation = Quaternion.identity;

        map_boxs.Add(box.transform.position,box);
    }
    //产生怪物
    public static GameObject SpawnMonster(string name)
    {
        GameObject monster=Pools.Instance.Spawn(name);
        monsters.Add(monster);
        return monster;

    }
    //搭建炮塔
    public static GameObject SpawnTowerr(string name,Vector3 towerPos)
    {

        GameObject tower= Pools.Instance.Spawn(name);
        tower.transform.position = towerPos;
        
        towers.Add(tower.transform.position, tower.GetComponent<Tower>());

        return tower;

    }
    //产生子弹
    public static GameObject SpawnBullet(string name, Vector3 initPos, Vector3 target,float speed)
    {
        GameObject bullet=Pools.Instance.Spawn(name);
        bullet.GetComponent<Bullet>().LoadInit(initPos, target,speed);
        bullets.Add(bullet);
        return bullet;
    }    

    #region 辅助方法
    public static bool IsTower(Vector3 pos)
    {
        foreach(var p in towers.Keys)
        {
            if (pos == p)
                return true;
        }
        return false;
    }
    public static bool IsMap(Vector3 pos)
    {
        foreach (var p in map_boxs.Keys)
        {
            if (pos == p)
                return true;
        }
        return false;
    }
    //清除spaner
    public static void MapClear()
    {
        foreach (var t in map_boxs.Values)
        {
            Pools.Instance.Unspawn(t);
        }
        map_boxs.Clear();
        towers.Clear();
        monsters.Clear();
    }
    #endregion
    static bool m_isAll=false;
    public static bool IsAll
    {
        get { return m_isAll; }
        set { m_isAll = value; }
    }
    public void SpawnMonster(GameObject bloodBar, Transform transform)
    {
        StaticData.CurrntSpaner(GameMain.Instance.currentProgress);
        StopAllCoroutines();
        StartCoroutine(Monster(bloodBar,transform,StaticData.arry));
    }
    IEnumerator<object> Monster(GameObject bloodBar,Transform transform,int[][] arry)
    {
        yield return new WaitForSeconds(5);
        for (int j = 0; j < arry.Length; j++)
        {
            Debug.Log(" arry.Length:"+arry.Length);
            for (int i = 0; i < arry[j].Length; i++)
            {
                Consts.MonsterType = arry[j][i];
                GameObject monster = Spawner.SpawnMonster("Warrior");
                GameObject blood = GameObject.Instantiate<GameObject>(bloodBar);
                blood.transform.SetParent(transform);
                blood.GetComponent<EnemyBlood>().Enemy = monster.GetComponent<Monster>();
                blood.GetComponent<EnemyBlood>().InitBloodBar();
                blood.SetActive(true);
                yield return new WaitForSeconds(1);
            }
            yield return new WaitForSeconds(3);
        }
        m_isAll = true;

    }
}
